#pragma once
#include <iostream>
#include "glad/glad.h"

static GLuint compileShader(const char* src, GLenum type) {
    GLuint vs = glCreateShader(type);
    glShaderSource(vs, 1, &src, nullptr);
    glCompileShader(vs);

    int bCompileSuc;
    glGetShaderiv(vs, GL_COMPILE_STATUS, &bCompileSuc);
    if(!bCompileSuc){
        char infoLog[512];
        glGetShaderInfoLog(vs, 512, nullptr, infoLog);
        std::cerr << "Compile shader failure: " << infoLog << std::endl;
        exit(-1);
    }
    return vs;
}

static GLuint getProgramFromShaderSource(const char* vsSource, const char* fsSource) {
    GLuint vs = compileShader(vsSource, GL_VERTEX_SHADER);
    GLuint fs = compileShader(fsSource, GL_FRAGMENT_SHADER);

    GLuint program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    int bLinkSuc;
    glGetProgramiv(program, GL_LINK_STATUS, &bLinkSuc);

    if(!bLinkSuc){
        char infoLog[512];
        glGetProgramInfoLog(program, 512, nullptr, infoLog);
        std::cerr << "Link shader failure: " << infoLog << std::endl;
        exit(-1);
    }

    glDeleteShader(vs);
    glDeleteShader(fs);
    return program;
}
